<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


    <el-form-item label="订单状态" prop="status">
         <el-select v-model="queryParams.status" placeholder="请选择" clearable>
           <el-option
             v-for="dict in dict.type.goods_order_status"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
      </el-form-item>



      <el-form-item label="创建时间">
          <el-date-picker size="small" v-model="uploadTime" @change="handleQuery" unlink-panels :picker-options="pickerOptions" 　 style="width: 220px" value-format="yyyy-MM-dd" type="daterange"
                          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:goodsOrders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:goodsOrders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:goodsOrders:remove']"
        >删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:goodsOrders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsOrdersList" @selection-change="handleSelectionChange">

      <el-table-column label="订单编号" align="center" prop="orderNo"  min-width="150"/>
      <el-table-column label="用户" align="center" prop="userName" min-width="120" />
      <el-table-column label="商品" align="center" prop="goodsName" min-width="220"/>
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="总价" align="center" prop="totalMoney" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>



      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:goodsOrders:edit']"
          >支付详情</el-button>

        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学员约课/退课对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
         <el-form-item label="总金额：" prop="totalMoney">
         {{form.totalMoney}}
        </el-form-item>
        <el-form-item label="支付金额：" prop="userId">
           {{form.payMoney}}
        </el-form-item>
        <el-form-item label="支付积分：" prop="goodsId">
          {{form.payPoint}}
        </el-form-item>
        <el-form-item label="积分兑换率" prop="price">
           {{form.pointRate}}
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.goods_order_pays_status" :value="form.status" />
        </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listGoodsOrders, getGoodsOrders, delGoodsOrders, addGoodsOrders, updateGoodsOrders } from "@/api/course/goodsOrders";
import { getGoodsOrderPayDetail} from "@/api/course/goodsOrderPays";

export default {
  name: "GoodsOrders",
  dicts: ['goods_order_status','goods_order_pays_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员约课/退课表格数据
      goodsOrdersList: [],
      uploadTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        userId: null,
        goodsId: null,
        price: null,
        num: null,
        totalMoney: null,
        status: null,
        payTime: null,
      },
      pay: {
        open:false,
        title:''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        goodsId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        totalMoney: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态：1待支付，2待核销，3已核销，4已取消不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.uploadTime[0] = this.getNearDay(-30);
    this.uploadTime[1] = this.getNearDay(0);
    this.getList();
  },
  methods: {
    /** 查询学员约课/退课列表 */
    getList() {
      this.loading = true;
       if (this.uploadTime) {
            this.queryParams.beginDate = this.uploadTime[0];
            this.queryParams.endDate = this.uploadTime[1];
        } else {
            this.queryParams.beginDate = '';
            this.queryParams.endDate = '';
        }
      listGoodsOrders(this.queryParams).then(response => {
        this.goodsOrdersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {

      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsOrderId: null,
        transactionId: null,
        outTradeNo: null,
        type: null,
        totalMoney: null,
        payMoney: null,
        payPoint: null,
        pointRate: null,
        status: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员约课/退课";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoodsOrderPayDetail(id).then(response => {
         this.form = response.data;
         this.open = true;
         this.title = "支付详情";
       });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoodsOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员约课/退课编号为"' + ids + '"的数据项？').then(function() {
        return delGoodsOrders(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/goodsOrders/export', {
        ...this.queryParams
      }, `goodsOrders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
